import { Component } from '@angular/core'
import { NavController, NavParams } from 'ionic-angular';

import { StudyComments } from '../../../../mocks/comments.mock';
import { StudyCommentMore } from '../study-comment-more/study-comment-more';
import { HaokurBasePage } from '../../haokur-base.page';
import { Api } from '../../../../providers/api-service';

@Component({
  selector: 'page-study-detail',
  templateUrl: 'study-detail.html'
})

export class StudyDetailPage extends HaokurBasePage {

  commentContent = ''
  tabType = 'intro'; // intro 介绍, menu 目录, ppt ppt
  lessMenu = [
    {
      no: '一',
      title: '销售准备',
      play_num: 291,
      time: '02:30',
      progress: '20',
      price: '0',
      has_buy: false,
    },
    {
      no: '二',
      title: '建立亲和',
      play_num: 100,
      time: '02:30',
      progress: '20',
      price: '1.00',
      has_buy: false,
    },
    {
      no: '三',
      title: '探寻需求',
      play_num: 66,
      time: '11:30',
      progress: '0',
      price: '1.00',
      has_buy: false,
    }
  ]

  ppts = [
    'https://s1.ax1x.com/2018/01/28/pjzfgO.png',
    'https://s1.ax1x.com/2018/01/28/pjzhvD.png',
    'https://s1.ax1x.com/2018/01/28/pjzIDH.png',
  ]

  comments = StudyComments
  showMoreActionFlag = false;
  lessonId = '';

  curriculumInfo = null
  curriculumChapter = null

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public api: Api,
  ) {
    super();
  }

  pageLoad() {
    this.lessonId = this.navParams.get('id')
    this.getCurriculumInfo()
    this.gettCurriculumChapter();
    this.getCommentList()
  }

  getCurriculumInfo() {
    this.api.postBody('V1/GetCurriculumDetail', {
      "CurriculumId": 1028 || this.lessonId,
    })
      .then(res => {
        this.curriculumInfo = res.Data;
      })
      .catch(err => {
        console.log(err)
      })
  }

  gettCurriculumChapter() {
    this.api.postBody('V1/GettCurriculumChapter', {
      "ID": 1028 || this.lessonId,
      "Stage": 1,
      "PageIndex": 1,
      "PageSize": 99
    })
      .then(res => {
        this.curriculumChapter = res.Data.Items;
      })
      .catch(err => {
        console.log(err)
      })
  }


  tabLessonAbout(type) {
    if (this.tabType === type) {
      return
    }
    this.tabType = type
  }

  goMoreComments() {
    this.navCtrl.push(StudyCommentMore, {
      id: 1
    })
  }

  showMoreActionModal() {
    this.showMoreActionFlag = true
  }

  hideMoreActionModal() {
    this.showMoreActionFlag = false
  }

  // 获取评论列表
  getCommentList(pIndex = 1) {
    this.api.postBody('V1/GetEvaluateList', {
      ActivityID: 1028 || this.lessonId,
      PageIndex: pIndex,
      PageSize: this.pSize,
    })
      .then(res => {
        if (res.data) {
          this.comments = res.data
        }
      })
      .catch(err => {
        console.log(err)
      })
  }

  // 插入评论
  commentStudy() {
    // http://demo.ilaizhan.com:8082/Help/Api/POST-V1-InsertEvaluate
    if (!this.commentContent) {
      return
    }
    this.api.postBody('V1/InsertEvaluate', {
      ObjID: 1028,
      Message: this.commentContent
    })
      .then(res => {
        console.log('评论成功')
      })
      .catch(err => {
        console.log(err)
      })
  }

}